<%
	//includep('db_subleme.tyle');
	[res].user = getRes(getUser!);
	// The resource parameter of the request is the current resource.
	.s = filterId(getp("resource"));
	[res].c = s=="" ? user : getRes(s);
	c!=() ? (s=c->getTitle(););
	.view = getp('view');
	.history = getv("history");
	*history = (history:!(*$!=()?$)) >> -8;
	.rl = c.!rightsLevel(user);
	.edit = view==() && rl>=can.@Edit ? getp('edit');
	.warnings = getv('warnings');
  .resType = resourceType(c);
%>
<%=pageHead%> 
<link href="genlayout.css" rel="stylesheet" type="text/css" />
<title><%=title%> - <%=view=="search"?"Resource search":c->getTitle()%></title>
<script language="javascript" type="text/javascript" src="script.js"></script>
<script language="javascript" type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script><%
	edit=="def" && resType==WebPageResource ? 
	( %>
<script language="javascript" type="text/javascript">
tinyMCE.init({
  editor_name: "<%=user->getTitle()%>",
  advimage_styles: 'bordered=wiki;unbordered=""',
  table_styles: "bordered=wiki;unbordered=",
  table_cell_styles: "stressed=x;normal="
});
</script> <%
	);%>
</head>
<body class="online">
<div class="logo">
	<div class="borderOpenAtTopRight">
		<div class="logoBox">
			<%=logo%>
		</div>
	</div>
</div>
<div class="navigator">
	<div class="borderOpenAtTop">
		<div class="navigatorBox">
			<form action="resourcerer.tsp" style="display:inline;" name="bannerForm">
			<input type="submit" value="Go to / create &rarr;" id="gotobtn" />
			<input type="text" name="resource" value="<%=user->getTitle()%>" /></form>
			<form style="display:inline;" action="search.tsp" onsubmit="resource.value=document.bannerForm.resource.value;"><input type="submit" value="&larr; Search" id="searchbtn" /><input type="hidden" name="resource" /></form>
				&nbsp;<font class="small">
					<a href="login.tsp<%=s!='Guest'?'?resource='+tyleToUrl(s)%>" class="action"><%=user==GuestResource?"Login":"Logout"%></a>
					<script>nonSecureLink(<%=port%>);</script><%
		history -= {c}; 
		.sep='';
		history:! ( %><%=sep%> <%=sep=',';resRef!%><% ); {helpRes}@history ? : ( %><%=sep%> <%=resRef(helpRes)%><%);%>
				</font>
		</div>
	</div>
</div><%
	warnings ? 
	(%>
<%=blockHead%>
	Warnings
<%=blockBody%>
	<font class="warning">
		<%=(warnings):!{$,'<br />'}%>
	</font>
<%=resourceBlockEnd%><%
		*warnings={};
	);
	/* MAIN BLOCK ----------------{ */
	c==() && view!="search" ? 
	(	.found=0; %>
<%=blockHead%>
	<%=s%>
<%=blockBody%><%
	"<%=s%>" does not exist.
	<%=(Search.queryName{s,user}:!{"<li>",resRef!,"\n"}).!($?{found=1;' Did you mean:<ul>',$,'</ul>'}:'<br /><br />')%><%
		.t={};
		AccessResource->getSubClasses(1):!
		( .id=$->getId();
			checkAction{{:'action'=>'create','resource'=>s,'o'=>id},user}?(t+={'<br /><input type="radio" name="o" value="',tyleToUrl(id),'"',*getv("lastCreateType")==id?' checked',' />',$->getTitle()};);
		);
		t ? (
			.file=getp('file');%>
	<div class="content">
		<form action="action.tsp" name="createForm" method="post" enctype="multipart/form-data">
		<input type="hidden" name="resource" value="<%=s%>" />
			<b><%=found?'Or do you want to c':'C'%>reate it with accessibility:</b><%=t%><br /><br />
			<b>And of Type:</b> 
		<input type="radio" name="type" value="web page" checked onClick="hideText('explain');" />Web page
		<input type="radio" name="type" value="binary" onClick="showText('explain');" <%=file!=()?'checked'%> />Attachment
		<input type="radio" name="type" value="raw" onClick="hideText('explain');" />Raw text
		<input type="radio" name="type" value="discussion" onClick="hideText('explain');" />Discussion
		<input type="radio" name="type" value="log" onClick="hideText('explain');" />Log
		<input type="radio" name="type" value="reference" onClick="hideText('explain');"/>Reference<br />
			<div id="explain" style="display:none;color:#aa0000">It is advised to use the file extension in the resource name (like "my dog.jpg"), so it can be automatically opened when downloading.</div>
			<br />Make sure the name is specific, as nobody else can use that name for other purposes.<br /><br /><%
			file!=() ? (%>
		<input type="file" name="definition" id="filepath" /><br /><br />
			<script language="JavaScript">
			document.getElementById('filepath').focus();
			</script><%
			);%>
		<input type="submit" name="action" value="Create" />
		</form>
	</div><%
		);%>
<%=blockEnd%><%
	) : (
		view=="search" ? (
		.query=getp("query");
		.results=Search.getQueryResult!;
		.resCount=@results;
		showMenu
		{ query==''?'All visible resources':'Resource search for "'+query+'"',
			{	{ 'Actions',
					{ resCount>0 && resCount<=40 ? '<a href="zip.tsp/file/result.zip" target="_blank" class="action" title="Download all found resources in a zip-file">Download</a>' 
					}
				}
			}
		};%>
<%=resourceBlockBegin%>
	<div class="content"><%=
			resCount ? 
			( resCount>40
				? 'Too many results. Please narrow your search.<br />'
				:	{ '<ul class="stats">',(results:!{"<li>",resRef!,"\n"}),'</ul>'
					}
			)
			: 'Nothing found.'%>
	</div>
<%=resourceBlockEnd%><% 			   
		) : 
		view=="history" ? (%>
<%=blockHead%>
	History of "<%=s%>"
<%=blockBody%>
		<form action="resourcerer.tsp"><input type="button" value="Back" onClick="history.back()" />
		</form><%
			getDefHistory{c,getp('offset'),10};%>
<%=blockEnd%><% 							   
		) :
		//normal viewing or editing:
		(
			rl>=can.@Read ?
			(	history +<= {c};
				.isBin=isBinary(c);
				rl>=can.@Edit && ( 'def'@edit || (isBin && dbinfo(defPath(c))==()) ) ? 
				(	.isReplyEdit = resType == DiscussionResource && !('ctrl'@edit); %>
	<form action="action.tsp" name="editingForm" method="post" <%=isBin?'enctype="multipart/form-data"'%>><%
	<input type="hidden" name="edit" value="<%=edit%>" /><%
					getp('chapter').!$!=()?(%>
	<input type="hidden" name="chapter" value="<%=$%>" /><%
					);%>
	<input type="hidden" name="starttime" value="<%=(""+(getDateTime!:!{$,'_'}))>>1%>" />
	<input type="hidden" name="resource" value="<%=tyleToUrl(c.id)%>" /><%
					showMenu
					{ s, 
						{ { 'File',
								{ '<input type="submit" name="action" value="Set" title="Save changes and return" />',
									.canMinor = !isBin && user!=GuestResource && resType != DiscussionResource&& resType != LogResource;
									canMinor ? '<input type="submit" name="action" value="Set Minor" title="Save minor changes and return"/><input type="hidden" name="edit" value="'+edit+'" />',
									canMinor ? '<input type="submit" name="action" value="Save Minor" title="Save minor changes"/>',
									'def'@edit ? '<input type="submit" name="action" value="Stop" title="Back to normal view" />',
								}
							},
							
							edit=='wikidef' || isReplyEdit ?
							{	'Insert',
								(
									isReplyEdit ? 
									{} : 
									{
							      "<input type=\"button\" onClick=\"addtext('= ',' =');\" value=\"Header1\" title=\"= Header1 =\" />",
							      "<input type=\"button\" onClick=\"addtext('== ',' ==');\" value=\"Header2\" title=\"== Header2 ==\" />",
							      "<input type=\"button\" onClick=\"addtext('=== ',' ===');\" value=\"Header3\" title=\"=== Header3 ===\" />",
							      "<input type=\"button\" onClick=\"addtext('----','');\" value=\"Line\" title=\"---- Horizontal line\" />",
							      "<input type=\"button\" onClick=\"addtext('* ','');\" value=\"Bullet1\" title=\"* Level 1 Bullet\" />",
							      "<input type=\"button\" onClick=\"addtext('** ','');\" value=\"Bullet2\" title=\"** Level 2 Bullet\" />",
							      "<input type=\"button\" onClick=\"addtext('# ','');\" value=\"Number1\"  title=\"# Level 1 Enumeration\" />",
							      "<input type=\"button\" onClick=\"addtext('## ','');\" value=\"Number2\" title=\"##  Level 2 Enumeration\" />"
									} 
								) +
								{
						      "<input type=\"button\" onClick=\"addtext('\\'\\'','\\'\\'');\" value=\"Italic\" title=\"''Emphasis (Italic)''\" />",
						      "<input type=\"button\" onClick=\"addtext('\\'\\'\\'','\\'\\'\\'');\" value=\"Bold\" title=\"'''Strong emphasis (Bold)'''\"/>",
						      "<input type=\"button\" onClick=\"addtext('[[name]]','');\" value=\"Link\" title=\"[[Link to other page]]\"/>",
						      "<input type=\"button\" onClick=\"addtext('[[http://url]]','');\" value=\"External\" title=\"[[Link to external web page]]\"/>",
						      "<input type=\"button\" onClick=\"addtext('[[http://url|text]]','');\" value=\"External+\" title=\"[[Link to web page|Visible text]]\"/>"
								}
							}
						}
					};%>
<%=resourceBlockBegin%><%
					isBin ? (%>
			<input type="file" name="definition" size="40" /> <%
					) : (%>
			<TEXTAREA name="definition" rows="29" style="width:99%;" onkeydown="blockKey(event);"><%=
				 txt2textarea(
					loadDef(c) .!
					isReplyEdit
					? discussionDictation{$,edit-'def'} 
					: 
					(	.ch=getp('chapter');
							( ch!=() ? getChapter(ch) : $ ) 
						.!
							edit=='wikidef'? html2wiki! : $ 
					)
			)%></TEXTAREA><%
					);%>	
	</form><%
				) : ( 
					showMenu
					{ s,
						{
							{'Edit',
								{	resType!=DiscussionResource && resType!=LogResource ? actUrl{'edit',user,{:'resource'=>c.id,'edit'=>'def'},'Normal Edit'} :
									rl>=can.@Control ? actUrl{'edit',user,{:'resource'=>c.id,'edit'=>'defctrl'},'Normal edit'},
									resType==WebPageResource ? actUrl{'edit',user,{:'resource'=>c.id,'edit'=>'rawdef'},'HTML edit'},
									resType==WebPageResource ? actUrl{'edit',user,{:'resource'=>c.id,'edit'=>'wikidef'},'Wiki edit'},
									actUrl{'rename',user,{:'resource'=>c.id},'Rename',(),'return rename(this);'}
								}
							},
							{'View',
								{ resType!=DiscussionResource || rl>=can.@Control ? rref{{:'resource'=>c.id,'view'=>'history'},'History','action'},
									ref{'view.tsp?'+params2url({:'total'=>1,'resource'=>c.id}+(getp('expand')!=()?{:'expand'=>getp('expand')})),'Print view',"action",1},
									ref{'view.tsp?'+params2url({:'resource'=>c.id}),'Plain view',"action",1},
									'<a href="search.tsp?resource=['+c->getTitle()+']" class="action">Referrers</a>'
								}
							},
							{'Action',
								{ actUrl{'clone',user,{:'resource'=>c.id},'Clone'},
									actUrl{'attach',user,{:'resource'=>c.id},'Attach file',(),"return attach(this,'"+tyleToUrl(c->getTitle())+"');"},
									ref{ {'remover.tsp?',params2url{:'resource'=>c.id}}, 'Remove...', 'action',0,'return popupRemover(this);' }
								}
							}
						}	
					};%>
<%=resourceBlockBegin%>
	<div class="textBox"><%=
					isBin ? (
						ref{'download.tsp/resource/'+tyleToUrl(c->getTitle()),'Download','action',1}
					) : (
						resDef2html { c, resType, user, rl }.!(rl>=can.@Edit ? addChapterLinks($,c,user) : $)
					)%>
	<div style="clear:both;"></div>
	</div><%
				); %>
	<div id="stats" style="position:relative;width:100%;"><%
				// *** show all statementgroups associated with the current resource, and show links to remove the complete statementgroup, or to add a new resource to a statementgroup
				// after each statementgroup-predicate, show all objects within this statementgroup, and add links to remove or to add a new object.
				[showStatParams].ssp=&{c,user,1,getp('expand'),0,0,'<p class="statHdr">Meta-statements:'};
				.url=actUrl{'addMetaStat',user,{:'resource'=>c.id},'add',(),popupActionString};
				url!=() ? (%>
	  <%=ssp.header%><%
					ssp.header='';%>
		<font class="small"><%=url%> <%=actUrl{'addMetaInvStat',user,{:'resource'=>c.id},'inverse add',(),popupActionString}%></font></p><%
				) : ( 
					ssp.header +='</p>';
				);
				ssp.header+='<ul class="stats">';
				.expand=getp('expand');%>
			<%=showStats{c->getMeta().stats,"",ssp,'Meta'}%> 
			<%=showStats{c->getMeta().invStats,"Inv",ssp,'Meta'}%><% 
				ssp.header==() ? (%></ul><%);
				ssp.header='<p class="statHdr">Statements:';
				url=actUrl{'addStat',user,{:'resource'=>c.id},'add',(),popupActionString};
				url!=() ? (%>
	  <%=ssp.header%><%
					ssp.header='';%>
		<font class="small"><%=url%> <%=actUrl{'addInvStat',user,{:'resource'=>c.id},'inverse add',(),popupActionString}%> <%=actUrl{'addStatMeta',user,{:'resource'=>c.id},'add meta',(),popupActionString}%> <%=actUrl{'addInvStatMeta',user,{:'resource'=>c.id},'inverse add meta',(),popupActionString}%></font></p><%
				) : ( ssp.header +='</p>'; );
				ssp.header+='<ul class="stats">';%>
			<%=showStats{c.stats,"",ssp}%>
			<%=showStats{c.invStats,"Inv",ssp}%><%
				*ssp.header==() ? (%></ul><%);%>
		<span id="rightunder"><font class="small"><%=ssp.expandible?actref{{:'resource'=>c.id,'expand'=>-1},'...'}%><%=ssp.removable?ref{'#','X','action',0,"javascript:showXs(this);"}%></font>
		</span>
	</div><%
			) : (%>
<%=blockHead%>
	<%=s%>
<%=blockEnd%>
<%=resourceBlockBegin%>
<h2 style="text-align:center">Hidden</h2><%
			);%>
<%=resourceBlockEnd%><%
		);
	); %>
	<div class="banner"><%=banner%></div>
</body>
</html>
